﻿using System;
using System.Windows.Markup;
using Localizer;

namespace Poet
{
    public class Localizing : MarkupExtension
    {
        static Localizing()
        {
            DefaultLocalization = new DefaultLocalization();
            CurrentLocalization = DefaultLocalization;
        }

        public Localizing()
        {
        }

        public Localizing(string key)
        {
            Key = key;
        }

        public static ILocalization DefaultLocalization { get; set; }

        public static ILocalization CurrentLocalization { get; set; }

        public string Key { get; set; }

        public string StringFormat { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var value = CurrentLocalization.GetLocalization(Key) ?? DefaultLocalization.GetLocalization(Key) ?? Key;
            return StringFormat != null ? string.Format(StringFormat, value) : value;
        }
    }
}
